package com.changgou.web.order.controller;

import com.changgou.entity.Result;
import com.changgou.order.feign.OrderFeign;
import com.changgou.order.pojo.Order;
import com.changgou.pay.feign.PayFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Map;

@Controller
@RequestMapping("/wxpay")
public class PayController {

    @Autowired
    private OrderFeign orderFeign;

    @Autowired
    private PayFeign payFeign;

    @GetMapping
    public String wxPay(@RequestParam("orderId") String orderId, Model model) {
        //查询订单是否存在
        Order order = orderFeign.findById(orderId).getData();
        if(order == null){
            return "fail";
        }
        //根据订单支付状态判断
        if(!"0".equals(order.getOrderStatus())){
            return "fail";
        }

        //调用payFeign统一下单的接口
        Result payResult = payFeign.nativePay(orderId, order.getPayMoney());
        if(payResult.getData()==null){
            return "fail";
        }

        //封装结构数据
        Map payMap = (Map) payResult.getData();
        payMap.put("orderId",orderId);
        payMap.put("payMoney",order.getPayMoney());

//        System.out.println(payMap);
        model.addAllAttributes(payMap);
        return "wxpay";
    }


    @GetMapping("/toPaySuccess")
    public String toPaySuccess(Integer payMoney,Model model){
        model.addAttribute("payMoney",payMoney);
        return "paysuccess";
    }

}
